package com.cpecc.dao.system;

import com.cpecc.model.system.Dep;
import com.cpecc.model.system.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Repository
public class UserDao{

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void addUser(User user) {
		sessionFactory.getCurrentSession().save(user);
	}
	
	public void save(User user){
		Session session=sessionFactory.getCurrentSession();
		session.save(user);
		session.flush();
	}
	
	public void save1(User user){		
		Session session=sessionFactory.openSession();
		session.save(user);
		session.flush();
		session.close();
	}

	@SuppressWarnings("unchecked")	
	public List<User> getUsersInDep(String depID) {
		String hql = "from User t where t.dep.id=? order by t.locked";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, depID);
		return query.list();
	}

	public boolean delUser(String id) {
		String hql = "delete User u where u.id=?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);		
		return (query.executeUpdate() > 0);
	}

	public User getUser(String id) {
		return (User) sessionFactory.getCurrentSession().get(User.class, id);		
	}

	public boolean updateUser(User user) {
		String hql = "update User u set u.userName=?,u.password=?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getUserName());
		query.setString(1, user.getPassword());		
		return (query.executeUpdate() > 0);
	}
	
	@SuppressWarnings("unchecked")
	public Set<String> getRoles(String username) {
		String hql="select t.role.roleName from User t where t.userName=?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, username);
		List<String> list=query.list();
		Set<String> set=new HashSet<String>();
		set.addAll(list);		
		String hql2="select t.dep.role.roleName from User t where t.userName=?";
		Query query2 = sessionFactory.getCurrentSession().createQuery(hql2);
		query2.setString(0, username);
		List<String> list2=query2.list();
		set.addAll(list2);
		return set;
	}

	@SuppressWarnings("unchecked")
	public User findByUsername(String username) {
		String hql = "from User u where u.userName=?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, username);
		List<User> list=query.list();
		if(list.size()>0)
			return list.get(0);
		else
			return null;
	}
	
	@SuppressWarnings("unchecked")
	public User findByUsername1(String username) {
		String hql = "from User u where u.userName=?";
		Session session=sessionFactory.openSession();
		Query query = session.createQuery(hql);
		query.setString(0, username);
		List<User> list=query.list();
		session.close();
		if(list.size()>0)
			return list.get(0);
		else
			return null;
	}
	
	public Dep getUserDep(User user){
		String hql="select t.dep from User t where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);		
		query.setString(0, user.getId());
		return (Dep)query.uniqueResult();
	}
	
	public boolean cover(User user){		
		Session session=sessionFactory.getCurrentSession();
		String hql="update User u set u.userName=?,u.password=?,u.name=?,u.email=?,u.role=?,u.dep=?,u.gangWei=? where u.id=?";
		Query query=session.createQuery(hql);
		query.setString(0, user.getUserName());
		query.setString(1, user.getPassword());
		query.setString(2, user.getName());
		query.setString(3, user.getEmail());
		query.setParameter(4, user.getRole());
		query.setParameter(5, user.getDep());
		query.setString(6, user.getGangWei());
		query.setString(7, user.getId());
		session.flush();
		return (query.executeUpdate()>0);		
	}
	
	@SuppressWarnings("unchecked")
	public User getMiMaZH(String userName,String email){
		String hql="from User u where u.userName=? and u.email=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, userName);
		query.setString(1, email);
		List<User> list=query.list();
		if(list.size()>0)
			return list.get(0);
		else
			return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getUserByDep(String depId){
		String hql="from User t where t.dep.id=? and t.locked = 0";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, depId);
		return query.list();
	}
	
	public boolean hasUser(Dep dep){
		String hql="from User t where t.dep.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, dep.getId());
		return query.list().size()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getBuMenLingDaos(User user){
		List<User> data = new ArrayList<User>();

		String depId = user.getDep().getId();
		List<User> list = getBuMenFuLingDao(depId);
		if (!CollectionUtils.isEmpty(list)) {
			data.addAll(list);
		}

		list = getBuMenFuLingDaoJianZhi(depId);
		if (!CollectionUtils.isEmpty(list)) {
			data.addAll(list);
		}

		if (user.getDep().getParent() != null) {
			depId = user.getDep().getParent().getId();
			list = getBuMenFuLingDao(depId);
			if (!CollectionUtils.isEmpty(list)) {
				data.addAll(list);
			}

			list = getBuMenFuLingDaoJianZhi(depId);
			if (!CollectionUtils.isEmpty(list)) {
				data.addAll(list);
			}
		}
		return data;
	}

	public List<User> getBuMenFuLingDao(String depId) {
		String hql="from User t where t.role.roleName='部门负责人' and t.dep.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, depId);
		return query.list();
	}

	private List<User> getBuMenFuLingDaoJianZhi(String depId) {
		String hql0="select t.user from JianZhi t where t.role.roleName='部门负责人' and t.dep.id=?";
		Query query0=sessionFactory.getCurrentSession().createQuery(hql0);
		query0.setString(0, depId);
		return query0.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getBuMenLingDaos2(User user){
		String hql="from User t where t.role.roleName='部门负责人' and t.gangWei='2' and t.dep.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getDep().getId());
		List<User> list=new ArrayList<User>();
		list.addAll(query.list());
		if(user.getDep().getParent()!=null){
			Dep parent=user.getDep().getParent();
			String hql1="from User t where t.role.roleName='部门负责人' and t.dep=?";
			Query query1=sessionFactory.getCurrentSession().createQuery(hql1);
			query1.setParameter(0, parent);
			list.addAll(query1.list());
			String hql2="select t.user from JianZhi t where t.role.roleName='部门负责人' and t.dep=?";
			Query query2=sessionFactory.getCurrentSession().createQuery(hql2);
			query2.setParameter(0, parent);
			list.addAll(query2.list());
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getAllCaiWuR(){
		String hql="from User t where t.role.roleName='财务'";
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getCaiMaiR(){
		String hql="from User t where t.role.roleName='采买人'";
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getGZGLY(){
		String hql="from User t where t.role.roleName='低值管理员'";
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getCGBMFZR(){
		String hql="select t.user from JianZhi t where t.quanXian='采购部门负责人'";
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}
	
	public boolean isCGBMFZR(User user){
		String hql="from JianZhi t where t.quanXian='采购部门负责人' and t.user=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, user);
		return query.list().size()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getGZGLY1(){
		Session session=sessionFactory.openSession();
		String hql="from User t where t.role.roleName='低值管理员'";
		List<User> list=session.createQuery(hql).list();
		session.close();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getXinYongHu(){
		String hql="from User t where t.dep.id='888888' and t.locked=false";
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}

	public List<User> getUsersByGangWei(String gangWei) {
		String hql="from User t where t.gangWei=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, gangWei);
		return query.list();
	}
	public void lock(User user){
		String hql="update User t set t.locked=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setBoolean(0, !user.isLocked());
		query.setString(1, user.getId());
		query.executeUpdate();
	}

	public List<User> getGKBMFZR() {
		List<User> glyList = getGZGLY();
		if (CollectionUtils.isEmpty(glyList)) {
			return null;
		}

		Set<String> depIdSet = new HashSet<>();
		List<User> list=new ArrayList<>();
		for (User gly : glyList) {
			String depId = gly.getDep().getId();
			if (!depIdSet.contains(depId)) {
				String hql="from User t where t.role.roleName='部门负责人' and t.dep.id=?";
				Query query=sessionFactory.getCurrentSession().createQuery(hql);
				query.setString(0, depId);
				list.addAll(query.list());
				depIdSet.add(depId);
			}
		}
		return list;
	}

	public List<User> getBMFZR(User user) {
		List<User> data = new ArrayList<User>();

		Dep dep = user.getDep();
		String depId = null;
		if (dep.getCengJi() == 1) {
			depId = dep.getId();
		} else if (dep.getCengJi() == 2) {
			if (dep.getParent() != null) {
				depId = dep.getParent().getId();
			}
		}

		if (StringUtils.hasText(depId)) {
			List<User> list = getBuMenFuLingDao(depId);
			if (!CollectionUtils.isEmpty(list)) {
				data.addAll(list);
			}

			list = getBuMenFuLingDaoJianZhi(depId);
			if (!CollectionUtils.isEmpty(list)) {
				data.addAll(list);
			}
		}
		return data;
	}
}
